<?php
class TICE_Crud_Newsletter extends PC4_Crud_Abstrato {
	
	public 
		$tabelaEmails, 
		$emailPor = 'no_reply@no_reply.com';
	
	function __construct($tabela = 'newsletters_envios', $tabelaEmails = 'newsletters_emails') {
		parent::__construct($tabela);
		$this->tabelaEmails = $tabelaEmails;
	}
	
	function insere($umRegistro, $debug = false) {
		$emails = $umRegistro['emails'];
		unset($umRegistro['emails']);
		$id = parent::insere($umRegistro, $debug);
		
		if (is_numeric($id)) {
			if (!empty($emails)) {
				foreach ($emails as &$r) {
					$r['id_newsletters'] = $id;
				}
				$mEmails = new PC4_Crud_Abstrato($this->tabelaEmails);
				$mEmails->insereVarios($emails, '', $debug);
			}
		}
		
		return $id;
	}
	
	function getEnviados($id, $limite = 30) {
		$mEmails = new PC4_Crud_Abstrato($this->tabelaEmails);
		$rs = $mEmails->consulta(array('status' => 'ENVIADO', 'id_newsletters' => $id), 1, $limite);
		return $rs;		
	}
	function getPendentes($id, $limite = 30) {
		$mEmails = new PC4_Crud_Abstrato($this->tabelaEmails);
		$rs = $mEmails->consulta(array('status' => 'PENDENTE', 'id_newsletters' => $id), 1, $limite);
		return $rs;		
	}
	
	function getStatusEnvios($id = 0) {
		$onde = array();
		if (!empty($id)) {
			$onde['id'] = $id;
		}
		$this->db->select("*,
			(SELECT COUNT(*) FROM newsletters_emails WHERE id_newsletters = n.id AND status = 'ENVIADO') AS enviados,
			(SELECT COUNT(*) FROM newsletters_emails WHERE id_newsletters = n.id AND status = 'PENDENTE') AS pendentes,
		");
		$rs = $this->oconsulta($onde, 'registro DESC');
		return $rs;
	}
	
	function proximoEmail($idEnvio) {
		$mEmails = new PC4_Crud_Abstrato($this->tabelaEmails);
		$this->db->where(array('id_newsletters' => $idEnvio));
		$r = $mEmails->ve('PENDENTE', 'status');
		return $r;
	}
	
	
	/**
	 * Inicia um novo envio
	 * @param array $destinatarios - [{nome, email}]
	 * @param string $texto - O texto do corpo do e-mail
	 * @param string $descricao - Uma descrição de controle do envio
	 */
	function novoEnvio($destinatarios, $texto, $assunto = '') {
		$registro = array(
			'emails' => $destinatarios,
			'texto' => $texto,
			'assunto' => $assunto,
		);
		return $this->insere($registro);
	}

	function envia($idEnvio) {
		$rEnvio = $this->ve($idEnvio);
		$emailSender = new PC4_Util_Email();
		$mEmails = new PC4_Crud_Abstrato($this->tabelaEmails);

		for ($i = 0; $i < 2 && ($email = $this->proximoEmail($idEnvio)) != null; $i++) {
			if (empty($email->email)) {
				continue;
			}

			// envia o e-mail
			$emailSender->sendMail($rEnvio->assunto, $rEnvio->texto, $email->email, $this->emailPor);
			// salva o e-mail enviado
			$this->db->where(array('id_newsletters' => $idEnvio));
			$mEmails->edita(array('status' => 'ENVIADO'), $email->email, 'email');
		}
	}
	
	function apaga($valor, $campo = 'id', $debug = false) {
		$mEmails = new PC4_Crud_Abstrato($this->tabelaEmails);
		$mEmails->apaga($valor, 'id_newsletters');		
		parent::apaga($valor, $campo, $debug);
	}
	function apagaVarios($valores, $campo = 'id', $debug = false) {
		$this->db->where_in('id_newsletters', $valores);
		$this->db->delete($this->tabelaEmails);
		parent::apagaVarios($valores, $campo, $debug);
	}
}